package com.xinggq.user.service.entity;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;

import java.time.LocalDateTime;

/**
 * 用户角色关联实体类
 *
 * @author system
 * @since 2025/1/10
 */
@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
@TableName("sys_user_role")
public class SysUserRoleEntity {

  /**
   * 关联ID，使用分布式ID生成器
   */
  @TableId(value = "id", type = IdType.AUTO)
  private Long id;

  /**
   * 用户ID
   */
  private Long userId;

  /**
   * 角色ID
   */
  private Long roleId;

  /**
   * 分配人ID
   */
  private Long assignedBy;

  /**
   * 分配时间
   */
  private LocalDateTime assignedAt;

  /**
   * 状态：1-正常，0-禁用
   */
  private Integer status;

  /**
   * 设置分配信息
   */
  public void setAssignInfo(Long assignerId) {
    this.assignedAt = LocalDateTime.now();
    this.assignedBy = assignerId;
    if (this.status == null) {
      this.status = 1; // 默认正常状态
    }
  }

  /**
   * 设置创建信息
   */
  public void setCreateInfo(Long creatorId) {
    setAssignInfo(creatorId);
  }

  /**
   * 检查关联是否有效
   */
  public boolean isValid() {
    return this.status != null && this.status == 1;
  }

}